package com.mccken.practice.top100.Top_1$20.Top_18;

/**
 * @program: mccken-collect
 * @description:
 * @author: mccken
 * @create: 2024-01-06 16:16
 **/
public class Rotate {

	// 翻转： 先水平翻转，再主对角线翻转
	public void rotate(int[][] matrix) {
		int n = matrix.length;
		// 水平翻转
		for (int i = 0; i < n / 2; i++) {
			for (int j = 0; j < n; j++) {
				int temp = matrix[i][j];
				matrix[i][j] = matrix[n-1-i][j];
				matrix[n-i-1][j] = temp;
			}
		}

		// 主对角线翻转
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < i; j++) {
				int temp = matrix[i][j];
				matrix[i][j] = matrix[j][i];
				matrix[j][i] = temp;
			}	
		}
		
	}

	// 选装
	public void rotate1(int[][] matrix) {
		int n = matrix.length;
		for (int i = 0; i < n / 2; i++) {
			for (int j = 0; j < (n + 1) / 2; j++) {
				int temp = matrix[i][j];
				matrix[i][j] = matrix[n - j - 1][i];
				matrix[n - j - 1][i] = matrix[n - i - 1][n - j - 1];
				matrix[n - i - 1][n - j - 1] = matrix[j][n - i - 1];
				matrix[j][n - i - 1] = temp;
			}
		}
	}
}
